#include "junix.h"
#include <sys/socket.h>
#include <sys/un.h>


int main(int argc, char **argv)
{
	if(argc==1)
		err_quit("Usage:%s file",argv[0] );

	struct sockaddr_un sun;
	memset(&sun,0,sizeof(sun));
	sun.sun_family=AF_UNIX;
	strcpy(sun.sun_path,argv[1]);
	int len = sizeof(sun.sun_family)+strlen(sun.sun_path);

	int sockfd=socket(AF_UNIX,SOCK_STREAM,0);
	if(sockfd==-1)
		err_sys("socket");

	char buf[1024];
	ssize_t sz;

	if( connect(sockfd,(struct sockaddr*)&sun,len) == -1)
		err_sys("connect");

	int fd=open(__FILE__,O_RDONLY);
	if(fd==-1)
		err_sys("open");

	while( (sz=read(STDIN_FILENO,buf,1024))>0)
		if( write(sockfd, buf,sz) != sz)
			err_sys("write");

	if(sz<0)
		err_sys("read");

	if( close(fd) == -1)
		err_sys("close");

	if( close(sockfd) == -1)
		err_sys("close");


	return 0;
}

